package com.hq.talk.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

import com.hq.lib_comview.common.utils.NetUtil;
import com.hq.lib_comview.common.utils.ToastUtil;
import com.hq.talk.R;
import com.hq.talk.view.ui.MainAty;

/**
 * 网络监听器
 */
public class NetBroadCastReciver extends BroadcastReceiver {

    public NetEvent mNetEvent = MainAty.mNetEvent;

    @Override
    public void onReceive(Context context, Intent intent) {
        //此处是主要代码，  
        //如果是在开启wifi连接和有网络状态下  
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            int netWorkState = NetUtil.getNetWorkState(context);
            //接口回调传过去状态的类型
            if (null != mNetEvent) {
                mNetEvent.onNetChange(netWorkState);
            }
            if (netWorkState == NetUtil.NETWORK_NONE) {
                ToastUtil.showToastById(context,R.string.no_net_work);
            }
        }
    }

    //自定义接口
    public interface NetEvent {
        public void onNetChange(int netMobile);
    }
}
